package com.flower.controller.buyer;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.flower.annotation.Log;
import com.flower.domain.vo.buyer.FavoriteVO;
import com.flower.service.FavoriteService;
import com.flower.utils.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 购买者端收藏信息
 */
@Slf4j
@RestController
@RequestMapping("/buyer/favorite")
public class FavoriteController {

    @Autowired
    private FavoriteService favoriteService;

    /**
     * 分页获取收藏列表
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    public AjaxResult getFavorites(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<FavoriteVO> favorites = favoriteService.getFavorites(page, pageSize);
        return AjaxResult.success(favorites);
    }

    /**
     * 添加收藏
     * @param productId
     * @return
     */
    @Log(name = "添加收藏")
    @PostMapping("/add/{productId}")
    public AjaxResult addFavorite(@PathVariable Integer productId) {
        favoriteService.addFavorite(productId);
        return AjaxResult.success();
    }

    /**
     * 取消收藏
     * @param favoriteId
     * @return
     */
    @Log(name = "取消收藏")
    @DeleteMapping("/{favoriteId}")
    public AjaxResult removeFavorite(@PathVariable Integer favoriteId) {
        favoriteService.removeFavorite(favoriteId);
        return AjaxResult.success();
    }

    /**
     * 根据商品ID取消收藏
     * @param productId
     * @return
     */
    @Log(name = "根据商品ID取消收藏")
    @DeleteMapping("/product/{productId}")
    public AjaxResult removeFavoriteByProductId(@PathVariable Integer productId) {
        favoriteService.removeFavoriteByProductId(productId);
        return AjaxResult.success();
    }

    /**
     * 检查是否已收藏
     * @param productId
     * @return
     */
    @GetMapping("/check/{productId}")
    public AjaxResult checkFavorite(@PathVariable Integer productId) {
        boolean isFavorite = favoriteService.checkFavorite(productId);
        return AjaxResult.success(isFavorite);
    }

    /**
     * 根据商品ID获取收藏信息
     * @param productId
     * @return
     */
    @GetMapping("/info/{productId}")
    public AjaxResult getFavoriteInfo(@PathVariable Integer productId) {
        FavoriteVO favorite = favoriteService.getFavoriteByProductId(productId);
        return AjaxResult.success(favorite);
    }
} 